package com.thinkgem.jeesite.modules.gen.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thinkgem.jeesite.modules.gen.GenConstants;
import com.thinkgem.jeesite.modules.gen.core.GenFileBean;
import com.thinkgem.jeesite.modules.gen.dao.GenSchemeMapper;
import com.thinkgem.jeesite.modules.gen.entity.*;
import com.thinkgem.jeesite.modules.gen.util.GenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 生成方案Service
 *
 * @author kongxiangsheng
 * @version 2013-10-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GenSchemeService extends ServiceImpl<GenSchemeMapper, GenScheme> implements IService<GenScheme> {
    @Autowired
    private GenTemplateService genTemplateService;
    @Autowired
    private GenTemplateItemService genTemplateItemService;
    @Autowired
    private GenTableService genTableService;
    @Autowired
    private GenProjectService genProjectService;
    @Autowired
    private GenProjectModuleService genProjectModuleService;
    @Autowired
    private GenTableColumnService genTableColumnService;

    public List<GenFileBean> generate(GenScheme genScheme) {
        // 组装GenScheme数据
        GenProject genProject = genProjectService.getWithChildren(genScheme.getProjectId());
        GenTemplate genTemplate = genTemplateService.getWithChildren(genScheme.getTemplateId());
        GenProjectModule genProjectModule = genProjectModuleService.getById(genScheme.getModuleId());
        genScheme.setGenProject(genProject);
        genScheme.setGenTemplate(genTemplate);
        genScheme.setGenProjectModule(genProjectModule);

        List<GenFileBean> result = new ArrayList<GenFileBean>();

        // 获取项目的基础字段
        List<String> baseColumns = Arrays.asList((genProject.getBaseColumns()).split(","));

        // 获取项目的树形基础字段
        List<String> baseTreeColumns = Arrays.asList((genProject.getBaseTreeColumns()).split(","));

        // 获取模板列表
        List<GenTemplateRel> templateRelList = genTemplate.getGenTemplateRelList();

        // 获取需要生成的表信息及字段信息，如果有子表，则需同时获取子表的信息
        GenTable genTable = genTableService.getForGenerate(genScheme.getGenTableId(), baseColumns, baseTreeColumns, genProject);

        // 生成子表模板代码
        for (GenTable childTable : genTable.getChildTableList()) {
            childTable.setParent(genTable);
            childTable.setColumnList(genTableColumnService.getByGenTableId(childTable.getId()));
            genScheme.setGenTableId(childTable.getId());
            genScheme.setGenTable(genTableService.getForGenerate(childTable.getId(), baseColumns, baseTreeColumns, genProject));
            Map<String, Object> childTableModel = GenUtils.getDataModel(genScheme);
            for (GenTemplateRel genTemplateRel : templateRelList) {
                if (GenConstants.RelScope.CURD_CHILD.equalsIgnoreCase(genTemplateRel.getScope())) {
                    GenTemplateItem genTemplateItem = genTemplateItemService.getById(genTemplateRel.getItemId());
                    result.add(GenUtils.generate(genScheme, genTemplateItem, childTableModel));
                }
            }
        }

        // 生成主表模板代码
        genScheme.setGenTableId(genTable.getId());
        genScheme.setGenTable(genTable);
        Map<String, Object> model = GenUtils.getDataModel(genScheme);
        for (GenTemplateRel genTemplateRel : templateRelList) {
            if (GenConstants.RelScope.CURD_MAIN.equalsIgnoreCase(genTemplateRel.getScope())) {
                GenTemplateItem genTemplateItem = genTemplateItemService.getById(genTemplateRel.getItemId());
                result.add(GenUtils.generate(genScheme, genTemplateItem, model));
            }
        }
        return result;
    }
}
